home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / asynchat.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  8KB  |  242 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. """A class supporting chat-style (command/response) protocols.
  5.  
  6. This class adds support for 'chat' style protocols - where one side
  7. sends a 'command', and the other sends a response (examples would be
  8. the common internet protocols - smtp, nntp, ftp, etc..).
  9.  
  10. The handle_read() method looks at the input stream for the current
  11. 'terminator' (usually '\\r\\n' for single-line responses, '\\r\\n.\\r\\n'
  12. for multi-line output), calling self.found_terminator() on its
  13. receipt.
  14.  
  15. for example:
  16. Say you build an async nntp client using this class.  At the start
  17. of the connection, you'll have self.terminator set to '\\r\\n', in
  18. order to process the single-line greeting.  Just before issuing a
  19. 'LIST' command you'll set it to '\\r\\n.\\r\\n'.  The output of the LIST
  20. command will be accumulated (using your own 'collect_incoming_data'
  21. method) up to the terminator, and then control will be returned to
  22. you - by calling your self.found_terminator() method.
  23. """
  24. import socket
  25. import asyncore
  26. from collections import deque
  27.  
  28. class async_chat(asyncore.dispatcher):
  29.     '''This is an abstract class.  You must derive from this class, and add
  30.     the two methods collect_incoming_data() and found_terminator()'''
  31.     ac_in_buffer_size = 4096
  32.     ac_out_buffer_size = 4096
  33.     
  34.     def __init__(self, conn = None):
  35.         self.ac_in_buffer = ''
  36.         self.ac_out_buffer = ''
  37.         self.producer_fifo = fifo()
  38.         asyncore.dispatcher.__init__(self, conn)
  39.  
  40.     
  41.     def collect_incoming_data(self, data):
  42.         raise NotImplementedError, 'must be implemented in subclass'
  43.  
  44.     
  45.     def found_terminator(self):
  46.         raise NotImplementedError, 'must be implemented in subclass'
  47.  
  48.     
  49.     def set_terminator(self, term):
  50.         '''Set the input delimiter.  Can be a fixed string of any length, an integer, or None'''
  51.         self.terminator = term
  52.  
  53.     
  54.     def get_terminator(self):
  55.         return self.terminator
  56.  
  57.     
  58.     def handle_read(self):
  59.         
  60.         try:
  61.             data = self.recv(self.ac_in_buffer_size)
  62.         except socket.error:
  63.             why = None
  64.             self.handle_error()
  65.             return None
  66.  
  67.         self.ac_in_buffer = self.ac_in_buffer + data
  68.         while self.ac_in_buffer:
  69.             lb = len(self.ac_in_buffer)
  70.             terminator = self.get_terminator()
  71.             None if not terminator else lb < n
  72.             terminator_len = len(terminator)
  73.             index = self.ac_in_buffer.find(terminator)
  74.             if index != -1:
  75.                 if index > 0:
  76.                     self.collect_incoming_data(self.ac_in_buffer[:index])
  77.                 
  78.                 self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
  79.                 self.found_terminator()
  80.                 continue
  81.             index = find_prefix_at_end(self.ac_in_buffer, terminator)
  82.             if index:
  83.                 if index != lb:
  84.                     self.collect_incoming_data(self.ac_in_buffer[:-index])
  85.                     self.ac_in_buffer = self.ac_in_buffer[-index:]
  86.                 
  87.                 break
  88.                 continue
  89.             self.collect_incoming_data(self.ac_in_buffer)
  90.             self.ac_in_buffer = ''
  91.  
  92.     
  93.     def handle_write(self):
  94.         self.initiate_send()
  95.  
  96.     
  97.     def handle_close(self):
  98.         self.close()
  99.  
  100.     
  101.     def push(self, data):
  102.         self.producer_fifo.push(simple_producer(data))
  103.         self.initiate_send()
  104.  
  105.     
  106.     def push_with_producer(self, producer):
  107.         self.producer_fifo.push(producer)
  108.         self.initiate_send()
  109.  
  110.     
  111.     def readable(self):
  112.         '''predicate for inclusion in the readable for select()'''
  113.         return len(self.ac_in_buffer) <= self.ac_in_buffer_size
  114.  
  115.     
  116.     def writable(self):
  117.         '''predicate for inclusion in the writable for select()'''
  118.         if self.ac_out_buffer == '' and self.producer_fifo.is_empty():
  119.             pass
  120.         return not (self.connected)
  121.  
  122.     
  123.     def close_when_done(self):
  124.         '''automatically close this channel once the outgoing queue is empty'''
  125.         self.producer_fifo.push(None)
  126.  
  127.     
  128.     def refill_buffer(self):
  129.         while len(self.producer_fifo):
  130.             p = self.producer_fifo.first()
  131.             if p is None:
  132.                 if not self.ac_out_buffer:
  133.                     self.producer_fifo.pop()
  134.                     self.close()
  135.                 
  136.                 return None
  137.             elif isinstance(p, str):
  138.                 self.producer_fifo.pop()
  139.                 self.ac_out_buffer = self.ac_out_buffer + p
  140.                 return None
  141.             
  142.             data = p.more()
  143.             if data:
  144.                 self.ac_out_buffer = self.ac_out_buffer + data
  145.                 return None
  146.             else:
  147.                 self.producer_fifo.pop()
  148.             data
  149.             return None
  150.             continue
  151.             return None
  152.  
  153.     
  154.     def initiate_send(self):
  155.         obs = self.ac_out_buffer_size
  156.         if len(self.ac_out_buffer) < obs:
  157.             self.refill_buffer()
  158.         
  159.         if self.ac_out_buffer and self.connected:
  160.             
  161.             try:
  162.                 num_sent = self.send(self.ac_out_buffer[:obs])
  163.                 if num_sent:
  164.                     self.ac_out_buffer = self.ac_out_buffer[num_sent:]
  165.             except socket.error:
  166.                 why = None
  167.                 self.handle_error()
  168.                 return None
  169.             except:
  170.                 None<EXCEPTION MATCH>socket.error
  171.             
  172.  
  173.         None<EXCEPTION MATCH>socket.error
  174.  
  175.     
  176.     def discard_buffers(self):
  177.         self.ac_in_buffer = ''
  178.         self.ac_out_buffer = ''
  179.         while self.producer_fifo:
  180.             self.producer_fifo.pop()
  181.  
  182.  
  183.  
  184. class simple_producer:
  185.     
  186.     def __init__(self, data, buffer_size = 512):
  187.         self.data = data
  188.         self.buffer_size = buffer_size
  189.  
  190.     
  191.     def more(self):
  192.         if len(self.data) > self.buffer_size:
  193.             result = self.data[:self.buffer_size]
  194.             self.data = self.data[self.buffer_size:]
  195.             return result
  196.         else:
  197.             result = self.data
  198.             self.data = ''
  199.             return result
  200.  
  201.  
  202.  
  203. class fifo:
  204.     
  205.     def __init__(self, list = None):
  206.         if not list:
  207.             self.list = deque()
  208.         else:
  209.             self.list = deque(list)
  210.  
  211.     
  212.     def __len__(self):
  213.         return len(self.list)
  214.  
  215.     
  216.     def is_empty(self):
  217.         return not (self.list)
  218.  
  219.     
  220.     def first(self):
  221.         return self.list[0]
  222.  
  223.     
  224.     def push(self, data):
  225.         self.list.append(data)
  226.  
  227.     
  228.     def pop(self):
  229.         if self.list:
  230.             return (1, self.list.popleft())
  231.         else:
  232.             return (0, None)
  233.  
  234.  
  235.  
  236. def find_prefix_at_end(haystack, needle):
  237.     l = len(needle) - 1
  238.     while l and not haystack.endswith(needle[:l]):
  239.         l -= 1
  240.     return l
  241.  
  242.